package top.mengchaob.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import top.mengchaob.deploy.api.DeployServiceI;
import top.mengchaob.deploy.table.HostServerDO;
import top.mengchaob.util.DeployConf;
import top.mengchaob.util.ResultBean;
import top.mengchaob.util.UtilTimeFormatter;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @title：
 * @author: mengchaob
 * @date: 2021年04月27日 14:44
 * @description:
 */
@Controller
@RequestMapping("traceController")
public class TraceController {
    @Autowired
    DeployServiceI deployServiceI;
    @RequestMapping(value = "trace",method = RequestMethod.TRACE)
    @ResponseBody
    public ResultBean addServer(@RequestBody HostServerDO hostServerDO, HttpServletRequest request){
        ResultBean rs = null;
        return ResultBean.ok("traceController trace OK");
    }
    @RequestMapping("delServer")
    @ResponseBody
    public ResultBean delServer(HttpServletRequest request){
        ResultBean rs = null;
        try {
            String ids = request.getParameter("ids");
            deployServiceI.delBatchByIds(ids);
            rs = ResultBean.ok("删除成功！");
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("删除失败！");
        }
        return rs;
    }
    @RequestMapping("fixServer")
    @ResponseBody
    public ResultBean fixServer(@RequestBody HostServerDO hostServerDO, HttpServletRequest request){
        ResultBean rs = null;
        try {
            hostServerDO.setCreateTime(UtilTimeFormatter.getTimeToYMDHMS(new Date().getTime()));
            hostServerDO.setHostDo(null);
            hostServerDO.setFileDo(null);
            deployServiceI.update(hostServerDO);
            rs = ResultBean.ok("保存成功！");
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("保存失败！");
        }
        return rs;
    }
    @RequestMapping("getServerInfoList")
    @ResponseBody
    public ResultBean getServerInfoList(){
        ResultBean rs = null;
        try {
            List list = deployServiceI.queryAll();
            rs = ResultBean.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("系统异常！");
        }
        return rs;
    }

    @RequestMapping("developByFile")
    @ResponseBody
    public ResultBean developByFile(HttpServletRequest rep){
        ResultBean rs = null;
        String id = rep.getParameter("id");
        try {
            if(DeployConf.get(id)!=null){
                rs = ResultBean.error("其他用户正在部署！");
            }else {
                DeployConf.put(id,id);
                deployServiceI.deployByFile(id);
                rs = ResultBean.ok("部署成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("系统异常！");
        }finally {
            DeployConf.remove(id);
        }
        return rs;
    }
    @RequestMapping("developByGit")
    @ResponseBody
    public ResultBean developByGit(HttpServletRequest rep){
        ResultBean rs = null;
        String id = rep.getParameter("id");
        try {
            if(DeployConf.get(id)!=null){
                rs = ResultBean.error("其他用户正在部署！");
            }else {
                DeployConf.put(id,id);
                deployServiceI.deployByGit(id);
                rs = ResultBean.ok("部署成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("系统异常！");
        }finally {
            DeployConf.remove(id);
        }
        return rs;
    }
}
